package com.nf.dao.impl;

import com.nf.dao.AdminInfoDao;
import com.nf.entity.AdminInfo;
import com.nf.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminInfoDaoImpl implements AdminInfoDao {
    @Override
    public AdminInfo findAdmin(String name, String pass) {
        AdminInfo admin=null;

        Connection conn= DBUtil.getConnection();
        String sql="select * from adminInfo where admin_name=? and password=?";

        PreparedStatement pstmt=null;
        ResultSet rst=null;
        try {
            pstmt=conn.prepareStatement(sql);

            pstmt.setString(1,name);
            pstmt.setString(2,pass);

            rst= pstmt.executeQuery();
            if(rst.next()){
                int id=rst.getInt(1);
                String adminName=rst.getString(2);
                String password=rst.getString(3);
                String level=rst.getString(4);
                String img=rst.getString(5);
                String registerTime=rst.getString(6);
                int status=rst.getInt(7);

                admin=new AdminInfo(id,adminName,password,level,img,registerTime,status);

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(rst,pstmt,conn);
        }
        return admin;
    }

    @Override
    public int updata(int id,String name) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="update adminInfo set admin_name=? where admin_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,name);
            pstmt.setInt(2,id);
            r=pstmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }
        return r;
    }

    @Override
    public int updatePass(int id, String pass) {
        int r=0;

        Connection conn=DBUtil.getConnection();
        String sql="update adminInfo set password=? where admin_id=?";
        PreparedStatement pstmt=null;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,pass);
            pstmt.setInt(2,id);
            r=pstmt.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(null,pstmt,conn);
        }
        return r;
    }
}
